<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>第二课 为什么要使用设计原则</title>
</head>

<body>
    <script>

        Function.prototype.setBefore = function(fn){
            var _this = this;
            return function(){
                fn.apply(this,arguments);
                var primitive = _this.apply(this,arguments);
                return primitive;
            }
        }

        Function.prototype.setAfter = function(fn){
            var _this = this;
            return function(){
                var primitive = _this.apply(this,arguments);
                fn.apply(this,arguments);
                return primitive;
            }
        }

        var getFnTime = (function(){
            var _startTime = 0;
            var _getSecond = function(time){
                var seConds = (time % (1000 * 60)) / 1000;
                return seConds + '秒';
            }
            var _getBeforeTime = function(){
                _startTime = new Date().getTime();
            }
            var _getAfterTime = function(){
                var endTime = new Date().getTime();
                console.log(_getSecond(endTime - _startTime));
            }
            var calculationFn = function(fn){
                fn.setBefore(function(){
                    _getBeforeTime();
                }).setAfter(function(){
                    _getAfterTime();
                })();
            }
            return {
                calculationFn:calculationFn
            }
        })();

        function fn1(){
            for(var i=0;i<1000;i++){
                document.body.innerHTML += i;
            }
        }
        function fn2(){
            for(var i=0;i<3000;i++){
                document.body.innerHTML += i;
            }            
        }
        getFnTime.calculationFn(fn1);
        getFnTime.calculationFn(fn2);
        // fn1();
        // fn2();
    </script>
</body>

</html>